package com.eleme.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eleme.user.dto.UserLoginDTO;
import com.eleme.user.dto.UserRegisterDTO;
import com.eleme.user.entity.User;
import com.eleme.user.mapper.UserMapper;
import com.eleme.user.service.UserService;
import com.eleme.user.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public String register(UserRegisterDTO dto) {
        // 用户名唯一性校验
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("user_name", dto.getUserName());
        if (userMapper.selectCount(query) > 0) {
            throw new RuntimeException("用户名已存在");
        }
        User user = new User();
        BeanUtils.copyProperties(dto, user);
        user.setUserId(UUID.randomUUID().toString().replace("-", ""));
        user.setCreateTime(new Date());
        user.setStatus(0);
        userMapper.insert(user);
        return "注册成功";
    }

    @Override
    public String login(UserLoginDTO dto) {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("user_name", dto.getUserName());
        User user = userMapper.selectOne(query);
        if (user == null || !user.getPassword().equals(dto.getPassword())) {
            throw new RuntimeException("用户名或密码错误");
        }
        if (user.getStatus() != null && user.getStatus() == 1) {
            throw new RuntimeException("用户已被禁用");
        }
        return "登录成功，欢迎 " + user.getUserName();
    }

    @Override
    public List<UserVO> listAll() {
        List<User> users = userMapper.selectList(null);
        List<UserVO> res = new ArrayList<>();
        for (User u : users) {
            UserVO vo = new UserVO();
            BeanUtils.copyProperties(u, vo);
            res.add(vo);
        }
        return res;
    }

    @Override
    public UserVO getById(String userId) {
        User user = userMapper.selectById(userId);
        if (user == null) return null;
        UserVO vo = new UserVO();
        BeanUtils.copyProperties(user, vo);
        return vo;
    }

    @Override
    public String updateUser(UserVO vo) {
        if (!StringUtils.hasText(vo.getUserId())) {
            throw new RuntimeException("用户ID不能为空");
        }
        User user = userMapper.selectById(vo.getUserId());
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        BeanUtils.copyProperties(vo, user);
        user.setUpdateTime(new Date());
        userMapper.updateById(user);
        return "用户信息更新成功";
    }

    @Override
    public String deleteUser(String userId) {
        int rows = userMapper.deleteById(userId);
        if (rows == 0) {
            throw new RuntimeException("用户不存在");
        }
        return "用户已删除";
    }
} 